/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dp1.pucp.Seguridad.DAO;

import com.dp1.pucp.Modelo.Clases.Usuario;
import com.dp1.pucp.Util.HibernateUtil;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class UsuarioDAO {

    public static String MD2 = "MD2";
    public static String MD5 = "MD5";
    public static String SHA1 = "SHA-1";
    public static String SHA256 = "SHA-256";
    public static String SHA384 = "SHA-384"; //Este algoritmo es el que se va a usar
    public static String SHA512 = "SHA-512";

    private static String toHexadecimal(byte[] digest) {

        String hash = "";
        for (byte aux : digest) {
            int b = aux & 0xff;
            if (Integer.toHexString(b).length() == 1) {
                hash += "0";
            }
            hash += Integer.toHexString(b);
        }
        return hash;
    }

    public static String getStringMessageDigest(String message) {
        byte[] digest = null;
        byte[] buffer = message.getBytes();
        try {
            MessageDigest messageDigest = MessageDigest.getInstance(SHA384);
            messageDigest.reset();
            messageDigest.update(buffer);
            digest = messageDigest.digest();
        } catch (NoSuchAlgorithmException ex) {
            System.out.println("Error creando Digest");
        }
        return toHexadecimal(digest);
    }

    public void guardarUsuario(Usuario usuario) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            session.saveOrUpdate(usuario);
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
        }
    }

    public ArrayList<String> listarEstado() {
        ArrayList<String> estados = new ArrayList<>();
        estados.add(0, "Seleccionar");
        estados.add(1, "ACTIVO");
        estados.add(2, "INACTIVO");
        estados.add(3, "BLOQUEADO");
        return estados;
    }
    /* aqui se obtiene una lista de empleados */

    public ArrayList<Usuario> listarUsuario() {
        ArrayList<Usuario> listaResultados = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            List lista = session.createQuery("from Usuario order by idusuario").list();
            Iterator iter = lista.iterator();
            while (iter.hasNext()) {
                Usuario p = (Usuario) iter.next();
                Hibernate.initialize(p.getIdusuario());
                Hibernate.initialize(p.getUsuario());
                Hibernate.initialize(p.getContrasena());
                Hibernate.initialize(p.getUltimaFechaIngreso());
                Hibernate.initialize(p.getContrasenaAnterior());
                Hibernate.initialize(p.getUltimaModificacion());
                Hibernate.initialize(p.getEstado());
                Hibernate.initialize(p.getPerfil());
                Hibernate.initialize(p.getEmpleado());
                Hibernate.initialize(p.getEmpleado().getIdempleado());
                listaResultados.add(p);
            }
            tx.commit();
        } catch (HibernateException e) {
            throw new HibernateException(e);
        } finally {
            session.close();
            return listaResultados;
        }
    }
}
